package onlinejudge.controller;

<<<<<<< Updated upstream
import java.io.File;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

=======
import com.alibaba.fastjson.JSONObject;
>>>>>>> Stashed changes
import onlinejudge.complier.CComplier;
import onlinejudge.complier.CplusplusComplier;
import onlinejudge.complier.JavaComplier;
import onlinejudge.entity.UserProblem;
import onlinejudge.service.ProblemService;
import onlinejudge.service.UserProblemService;
import onlinejudge.util.ComplierUtil;
import onlinejudge.util.DateUtil;
import onlinejudge.util.FileUtil;
import onlinejudge.vo.ProblemVo;
<<<<<<< Updated upstream
=======
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
>>>>>>> Stashed changes

@Controller
@RequestMapping("/code")
public class CodeController {
	
	@Resource
	private ProblemService problemService;
	@Resource
	private UserProblemService userProblemService;
	
	/**
	 * 运行代码接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/run")
	public String runCode(@RequestBody Map<String,Object> map,HttpServletRequest request) {
		String inputData=(String) map.get("inputData");
		String code=(String) map.get("itemCode");
		String codeType=(String) map.get("codeType");
		
		//调用编译器,返回结果
		String outputData=null;
		boolean runFlag=true;
<<<<<<< Updated upstream
		String path=request.getSession().getServletContext().getRealPath("/");
=======
//		String path=request.getSession().getServletContext().getRealPath("/");
		String path="D:\\JavaWeb\\OJ\\src\\main\\webapp\\";
//		System.out.println(path);
>>>>>>> Stashed changes
		if(codeType.equals("c")) {
			FileUtil.writeFile(code, path+"cCode"+File.separator+"main.c");
			CComplier cComplier=new CComplier(path+"cCode"+File.separator, inputData);
			cComplier.complier();
			String proError=cComplier.getErrorMsg();
			if(proError!=null&&!proError.equals("")) {
				outputData=proError;
				runFlag=false;
			}else {
				outputData=cComplier.getOutputData();
			}
		}else if(codeType.equals("cpp")) {
			FileUtil.writeFile(code, path+"c++Code"+File.separator+"main.cpp");
			CplusplusComplier cplusplusComplier=new CplusplusComplier(path+"c++Code"+File.separator, inputData);
			cplusplusComplier.complier();
			String proError=cplusplusComplier.getErrorMsg();
			if(proError!=null&&!proError.equals("")) {
				outputData=proError;
				runFlag=false;
			}else {
				outputData=cplusplusComplier.getOutputData();
			}
		}else if(codeType.equals("java")){
			FileUtil.writeFile(code, path+"javaCode"+File.separator+"Main.java");
			JavaComplier javaComplier=new JavaComplier(path+"javaCode"+File.separator, inputData);
			javaComplier.complier();
			String proError=javaComplier.getErrorMsg();
			if(proError!=null&&!proError.equals("")) {
				outputData=proError;
				runFlag=false;
			}else {
				outputData=javaComplier.getOutputData();
			}
		}
		
		JSONObject json=new JSONObject();
		json.put("outputData", outputData);
		json.put("runFlag",runFlag);
		return json.toJSONString();
	}
	/**
	 * 提交代码接口
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/submit")
	public String submitCode(@RequestBody Map<String,Object> map,HttpServletRequest request) {
		//用户Id,用来添加用户题目表
		@SuppressWarnings("unused")
		Integer userId=(Integer) map.get("userId");
		Integer problemId=(Integer) map.get("problemId");
		String code=(String) map.get("itemCode");
		String codeType=(String) map.get("codeType");
		
		ProblemVo problem=problemService.getProblem(problemId);
		
		//评测完之后，如果正确，把代码放到数据库里，并添加正确记录，否则添加错误记录，不把代码放到数据库里
		//与数据库里的测试样例比对，如果都通过，则正确，有一个不通过，则不正确
		//三种情况：答案错误、编译错误、正确
		String res=null;
		String path=request.getSession().getServletContext().getRealPath("/");
		if(codeType.equals("c")) {
			FileUtil.writeFile(code, path+"cCode"+File.separator+"main.c");
			res=ComplierUtil.cComplierjudge(path+"cCode"+File.separator, problem);
		}else if(codeType.equals("cpp")) {
			FileUtil.writeFile(code, path+"c++Code"+File.separator+"main.cpp");
			res=ComplierUtil.cppComplierjudge(path+"c++Code"+File.separator, problem);
		}else if(codeType.equals("java")){
			FileUtil.writeFile(code, path+"javaCode"+File.separator+"Main.java");
			res=ComplierUtil.javaComplierjudge(path+"javaCode"+File.separator, problem);
		}
		
		UserProblem userProblem=new UserProblem();
		userProblem.setUserId(userId);
		userProblem.setProblemId(problemId);
		String htmlCode=code;
		htmlCode=htmlCode.replace(">", "&gt;");
		htmlCode=htmlCode.replace("<", "&lt;");
		userProblem.setCode(htmlCode);
		userProblem.setSubmitDate(DateUtil.getDate());
		if(res.equals("success")) {
			userProblem.setStatus("success");
		}else {
			userProblem.setStatus("error");
		}
		//先从数据库中找该题是否做过
		//如果做过,更新数据库
		if(userProblemService.judgeUserProblem(userProblem)) {
			userProblemService.updateUserProblem(userProblem);
		}else {
			//如果没做过
			//添加记录到数据库
			userProblemService.addUserProblem(userProblem);
		}
		
		JSONObject json=new JSONObject();
		json.put("runRes",res);
		return json.toJSONString();
	}
}
